///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package mainpackage;
//
//import java.awt.AWTException;
//import java.awt.Image;
//import java.awt.MouseInfo;
//import java.awt.Point;
//import java.awt.Robot;
//import java.awt.Toolkit;
//import java.awt.event.InputEvent;
//import java.awt.event.KeyEvent;
//import java.awt.image.BufferedImage;
//import java.io.DataInputStream;
//import java.io.DataOutputStream;
//import java.io.IOException;
//import javax.microedition.io.StreamConnection;
//import javax.swing.ImageIcon;
//
///**
// *
// * @author MH
// */
//public class HandleDevice {
//
//    private DataInputStream in;
//    private DataOutputStream out;
//    private Robot robot;
//    private int mobileWidth, mobileHight;
//    private int screanCaptureRatio = 1;
//    private int mouseStep = 5;
//    private Variables variables;
//    private boolean mobileLost=false;
//
//    public void start(StreamConnection conn) {
//        variables = new Variables();
//        try {
//            out = conn.openDataOutputStream();
//            in = conn.openDataInputStream();
//        } catch (IOException ex) {
//            handleException(ex, "input & output");
//        }
//        startRecieving();
//    }
//
//    public void startRecieving() {
//        new Thread() {
//
//            @Override
//            public void run() {
//                try {
//                    int flag = in.readInt();
//                    switch (flag) {
//                        case Variables.FLAG_TEXT:
//                            String text = in.readUTF();
//                            //handle text
//                            sendScreenCapture();
//                            break;
//                        case Variables.FLAG_MOUSE:
//                            moveMouse(in.readInt());
//                            break;
//                        case Variables.FLAG_KEY:
//                            keyResponse(in.readInt());
//                            break;
//                        case Variables.FLAG_GET_IMAGE:
//                            mobileWidth = in.readInt();
//                            mobileHight = in.readInt();
//                            sendScreenCapture();
//                            break;
//                        case Variables.FLAG_SETTING:
//                            int settingType = in.readInt();
//                            if (settingType == 0) {
//                                changeWidthAndHight();
//                            }
//                            break;
//                        case Variables.FLAG_IMAGE:
//                            int length = in.readInt();
//                            byte[] bytes = new byte[length];
//                            in.readFully(bytes, 0, length);
//                            Image image = ImageTools.getImage(bytes);
//                            ImageFrame imageFrame=new ImageFrame();
//                            image=ImageTools.getScaledImage(image, imageFrame.getImageLabel().getWidth(), imageFrame.getImageLabel().getHeight());
//                            imageFrame.getImageLabel().setIcon(new ImageIcon(image));
//                            imageFrame.setVisible(true);
//                            break;
//                    }
//                } catch (IOException ex) {
//                    handleException(ex, "recieving");
//                    mobileLost=true;
//                }
//                if(mobileLost) new ServerClass().startServer();
//                else startRecieving();
//            }
//        }.start();
//    }
//
//    private void changeWidthAndHight() {
//        try {
//            mobileWidth = in.readInt();
//            mobileHight = in.readInt();
//        } catch (IOException ex) {
//            handleException(ex, "reading w & h");
//        }
//    }
//
//    private void moveMouse(int key) {
//        try {
//            robot = new Robot();
//            Point locationOnScreen = MouseInfo.getPointerInfo().getLocation();
//            if (key == variables.MOUSE_UP) {
//                if (mobileWidth < mobileHight) {
//                    robot.mouseMove(locationOnScreen.x - mouseStep, locationOnScreen.y);
//                } else {
//                    robot.mouseMove(locationOnScreen.x, locationOnScreen.y - mouseStep);
//                }
//            } else if (key == variables.MOUSE_DOWN) {
//                if (mobileWidth < mobileHight) {
//                    robot.mouseMove(locationOnScreen.x + mouseStep, locationOnScreen.y);
//                } else {
//                    robot.mouseMove(locationOnScreen.x, locationOnScreen.y + mouseStep);
//                }
//            } else if (key == variables.MOUSE_LEFT) {
//                if (mobileWidth < mobileHight) {
//                    robot.mouseMove(locationOnScreen.x, locationOnScreen.y + mouseStep);
//                } else {
//                    robot.mouseMove(locationOnScreen.x - mouseStep, locationOnScreen.y);
//                }
//            } else if (key == variables.MOUSE_RIGHT) {
//                if (mobileWidth < mobileHight) {
//                    robot.mouseMove(locationOnScreen.x, locationOnScreen.y - mouseStep);
//                } else {
//                    robot.mouseMove(locationOnScreen.x + mouseStep, locationOnScreen.y);
//                }
//            } else if (key == variables.MOUSE_LEFT_CLICK) {
//                robot.mousePress(InputEvent.BUTTON1_MASK);
//                robot.mouseRelease(InputEvent.BUTTON1_MASK);
//            } else if (key == variables.MOUSE_RIGHT_CLICK) {
//                robot.mousePress(InputEvent.BUTTON3_MASK);
//                robot.mouseRelease(InputEvent.BUTTON3_MASK);
//            }
//        } catch (AWTException ex) {
//            handleException(ex, "mouse move");
//        }
//    }
//
//    private void keyResponse(int key) {
//        if (key == variables.KEY_H) {
//            screanCaptureRatio *= 2;
//        } else if (key == variables.KEY_S) {
//            if (screanCaptureRatio != 1) {
//                screanCaptureRatio /= 2;
//            }
//        } else if (key == variables.KEY_8) {
//            mouseStep *= 2;
//        } else if (key == variables.KEY_0) {
//            if (mouseStep != 5) {
//                mouseStep /= 2;
//            }
//        } else if (key == variables.KEY_2) {
//            if (mobileWidth < mobileHight) {
//                robot.keyPress(KeyEvent.VK_LEFT);
//                robot.keyRelease(KeyEvent.VK_LEFT);
//            } else {
//                robot.keyPress(KeyEvent.VK_UP);
//                robot.keyRelease(KeyEvent.VK_UP);
//            }
//        } else if (key == variables.KEY_5) {
//            if (mobileWidth < mobileHight) {
//                robot.keyPress(KeyEvent.VK_RIGHT);
//                robot.keyRelease(KeyEvent.VK_RIGHT);
//            } else {
//                robot.keyPress(KeyEvent.VK_DOWN);
//                robot.keyRelease(KeyEvent.VK_DOWN);
//            }
//        } else if (key == variables.KEY_4) {
//            if (mobileWidth < mobileHight) {
//                robot.keyPress(KeyEvent.VK_DOWN);
//                robot.keyRelease(KeyEvent.VK_DOWN);
//            } else {
//                robot.keyPress(KeyEvent.VK_LEFT);
//                robot.keyRelease(KeyEvent.VK_LEFT);
//            }
//        } else if (key == variables.KEY_6) {
//            if (mobileWidth < mobileHight) {
//                robot.keyPress(KeyEvent.VK_UP);
//                robot.keyRelease(KeyEvent.VK_UP);
//            } else {
//                robot.keyPress(KeyEvent.VK_RIGHT);
//                robot.keyRelease(KeyEvent.VK_RIGHT);
//            }
//        } else if (key == variables.KEY_7) {
//            robot.keyPress(KeyEvent.VK_F5);
//            robot.keyRelease(KeyEvent.VK_F5);
//        } else if (key == variables.KEY_9) {
//            robot.keyPress(KeyEvent.VK_SHIFT);
//            robot.keyPress(KeyEvent.VK_F5);
//            robot.keyRelease(KeyEvent.VK_F5);
//            robot.keyRelease(KeyEvent.VK_SHIFT);
//        } else if (key == variables.KEY_1) {
//            robot.keyPress(KeyEvent.VK_ENTER);
//            robot.keyRelease(KeyEvent.VK_ENTER);
//        } else if (key == variables.KEY_3) {
//            robot.keyPress(KeyEvent.VK_ESCAPE);
//            robot.keyRelease(KeyEvent.VK_ESCAPE);
//        }
//    }
//
//    public void sendScreenCapture() {
//        new Thread() {
//
//            @Override
//            public void run() {
//                try {
//                    int wantedWidth = Toolkit.getDefaultToolkit().getScreenSize().width / screanCaptureRatio;
//                    int wantedHight = Toolkit.getDefaultToolkit().getScreenSize().height / screanCaptureRatio;
//                    int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
//                    int screenHight = Toolkit.getDefaultToolkit().getScreenSize().height;
//                    int mouseX = MouseInfo.getPointerInfo().getLocation().x;
//                    int mouseY = MouseInfo.getPointerInfo().getLocation().y;
//                    int x1, y1, mobileMouseX, mobileMouseY;
//
//                    if (mouseX < wantedWidth / 2) {
//                        x1 = 0;
//                    } else if (screenWidth - mouseX < wantedWidth / 2) {
//                        x1 = screenWidth - wantedWidth;
//                    } else {
//                        x1 = mouseX - (wantedWidth / 2);
//                    }
//
//                    if (mouseY < wantedHight / 2) {
//                        y1 = 0;
//                    } else if (screenHight - mouseY < wantedHight / 2) {
//                        y1 = screenHight - wantedHight;
//                    } else {
//                        y1 = mouseY - (wantedHight / 2);
//                    }
//
//                    BufferedImage screenCapture = ImageTools.captureScreen(x1, y1, wantedWidth, wantedHight);
//
//                    if (mobileWidth < mobileHight) {
//                        screenCapture = ImageTools.toBufferedImage(screenCapture.getScaledInstance(wantedHight, wantedHight, BufferedImage.SCALE_SMOOTH));
//                        screenCapture = ImageTools.rotate(screenCapture, 90);
//                        mobileMouseY = (int) ((float) (mouseX - x1) / (float) ((float) wantedWidth / (float) mobileHight));
//                        mobileMouseX = mobileWidth - ((int) ((float) (mouseY - y1) / (float) ((float) wantedHight / (float) mobileWidth)));
//                    } else {
//                        mobileMouseX = (int) ((float) (mouseX - x1) / (float) ((float) wantedWidth / (float) mobileWidth));
//                        mobileMouseY = (int) ((float) (mouseY - y1) / (float) ((float) wantedHight / (float) mobileHight));
//                    }
//
//                    byte[] bytes = ImageTools.getBytes(ImageTools.getScaledImage(screenCapture, mobileWidth, mobileHight));
//
//                    out.writeInt(Variables.FLAG_IMAGE);
//                    out.writeInt(mobileMouseX);
//                    out.writeInt(mobileMouseY);
//                    out.writeInt(bytes.length);
//                    out.write(bytes);
//                    out.flush();
//                } catch (Exception ex) {
//                    handleException(ex, "send capture");
//                }
//            }
//        }.start();
//    }
//
//    private void handleException(Exception ex,String errorType) {
//        System.out.println("error in "+errorType+": " + ex.getMessage());
//    }
//}